package com.zyw.controller.procurement;


import com.zyw.entity.*;
import com.zyw.from.ProcurementFrom;
import com.zyw.service.order.OrderService;
import com.zyw.service.procurement.ProcurementPurchaseOrdersService;
import com.zyw.service.repertory.RepertoryMaterialService;
import com.zyw.util.ResultVOUtil;
import com.zyw.util.UnitConversionUtil;
import com.zyw.vo.LayUITableVO;
import com.zyw.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/procurement/purchaseOrders")
public class procurementPurchaseOrdersController {



    @Autowired
    public OrderService orderService;

    @Autowired
    public RepertoryMaterialService repertoryMaterialService;
    @Autowired
    public ProcurementPurchaseOrdersService procurementPurchaseOrdersService;

    //菜品类别展示
    @GetMapping("/index")
    public String index(Model model){


        return "procurement/purchaseOrders/index";

    }
    //获取菜品类别信息
    @GetMapping("/list")
    @ResponseBody
    public LayUITableVO<List<ProcurementPurchaseOrders>> list (@RequestParam(defaultValue = "1") Integer page,
                                                       @RequestParam(defaultValue = "10") Integer limit,
                                                       ProcurementFrom from){

        Map<String ,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        map.put("from",from);
        LayUITableVO< List<ProcurementPurchaseOrders>> vo = procurementPurchaseOrdersService.findPage(map);


        return vo;
    }

    //修改菜品类别信息1（查询指定id菜品分类详情）
    @GetMapping("/edit-ui")
    public String editUI(String id,Model model){


        ProcurementPurchaseOrders procurementPurchaseOrders = procurementPurchaseOrdersService.selectById(id);

        model.addAttribute("procurementPurchaseOrders",procurementPurchaseOrders);

        return "procurement/purchaseOrders/edit";

    }

    @PostMapping("/edit")
    @ResponseBody
    public ResultVO edit(ProcurementPurchaseOrders procurementPurchaseOrders){


        procurementPurchaseOrdersService.updateNum(procurementPurchaseOrders);

        return ResultVOUtil.ok();
    }

    //修改菜品类别信息1（查询指定id菜品分类详情）
    @GetMapping("/add-ui")
    public String addUI(Model model){

        List<Order> orderList = orderService.selectByStatus(2);

     List<RepertoryMaterial> repertoryMaterialList =  repertoryMaterialService.selectChild();

        model.addAttribute("orderList",orderList);
        model.addAttribute("repertoryMaterialList",repertoryMaterialList);
        return "procurement/purchaseOrders/add";

    }


    @PostMapping("/add")
    @ResponseBody
    public ResultVO add(ProcurementPurchaseOrders procurementPurchaseOrders){

        procurementPurchaseOrders.setGeneratedDate(UnitConversionUtil.nowDate());
        procurementPurchaseOrders.setStatus(0);

        procurementPurchaseOrdersService.save(procurementPurchaseOrders);

        return ResultVOUtil.ok();
    }


    @PostMapping("/editStatus")
    @ResponseBody
    public ResultVO editStatus(ProcurementPurchaseOrders procurementPurchaseOrders){

        procurementPurchaseOrders.setStorageDate(UnitConversionUtil.nowDate());
        procurementPurchaseOrders.setStatus(1);
        //TODO 当前登录用户
        procurementPurchaseOrders.setStoragePeople("小兰");

        procurementPurchaseOrdersService.updateLog(procurementPurchaseOrders);


        return ResultVOUtil.ok();
    }
}
